clear
clear mata
clear matrix

capture log close
log using "log/results-nber.log", replace

mkdir tmp/

*       Patriotism!

*       FIRST VERSION  June	       14, 2022
*       THIS VERSION   June        14, 2022
*       LAST RUN       January     14, 2022

*       AUTHOR              AT
*       LAST REVISOR		

*       Log of revisions:

*       This produces tables and figures in the main text and appendix coming from NBER dataset

***************************************************************************************************
****                              PLAN OF THE PROCEDURE                                        ****
****___________________________________________________________________________________________****
****                                                                                           ****
****     								A. Main                                                ****
****     I. Tables                                                                             ****
****         Tab III. New Deal Support and Patriotism                                          ****
****         	a. Panel A                                     							       ****
****         	b. Panel B                                     							       ****
****         	c. Panel C                                    							       ****
****         Tab V. Identification Table: Individual-Level                                     ****
****         	a. Col. 1 & 3                                  							       ****
****     								B. Appendix                                            ****
****     I. Tables                                                                             ****	
****         Tab 7. Impact of WPA Employment and Unemployment                                  ****
****     II. Figures                                                                           ****	
****         Fig 8. Plausibly Exogenous (Conley et al., 2012)                                  ****
****___________________________________________________________________________________________****
***************************************************************************************************

macro def controls_mat       "i.C40AGE i.bpl C40NONWHITE C40IMMIGRANT iC40INCOME C40SCHOOL_E C40SCHOOL_H C40SCHOOL_C C40MARRIED C40FARM icommonlast icommonfirst ilengthlast ilengthfirst"
macro def controls_mat_noage                "C40NONWHITE C40IMMIGRANT iC40INCOME C40SCHOOL_E C40SCHOOL_H C40SCHOOL_C C40MARRIED C40FARM icommonlast icommonfirst ilengthlast ilengthfirst"

use "patriotism-nber", clear                                                    /* This data is stored on the NBER databases */

lab var C40WPA       				   "Works for WPA"
lab var C40UNEMP     				   "Unemployed"
lab var farmowner    				   "Farm owner"
lab var served       				   "Served in the army"
lab var C40NONWHITE  				   "Non white"
lab var C40IMMIGRANT 				   "Immigrant"
lab var iC40INCOME   				   "1939 income (i.h.s.)"
lab var C40SCHOOL_E  				   "Elementary school"
lab var C40SCHOOL_H  				   "High school"
lab var C40SCHOOL_C  				   "College"
lab var C40MARRIED   				   "Ever married"
lab var C40FARM      				   "Lives on a farm"
lab var icommonlast  				   "log surname commonness"
lab var icommonfirst 				   "log name commonness"
lab var ilengthlast  				   "Surname length"
lab var ilengthfirst 				   "Name length"

lab var iAAA_PF                       "AAA grants"
lab var iAAA_PF_farmowner             "AAA grants $\times$ farm owner"
lab var iSUM3MO_DROUGHT3340_farmowner "Summer droughts: 1933-40 $\times$ farm owner"
lab var iAGRI_T73_farmowner           "Tenure in Agri committee: 1933-35 $\times$ farm owner"
lab var iHOLC_PH                      "HOLC per home-owner"
lab var iHOLC_PH                      "HOLC loans"
lab var iHOLC_PH_C40HOMEOWN           "HOLC loans $\times$ home owner"
lab var C40HOMEOWN                    "Home owner"

do "code-build/01_Shopping_List.do"

bysort countyn: gen id_ind=_n
xtset  countyn id_ind

***************************************************************************************************
****     								A. Main                                                ****
****     I. Tables                                                                             ****
****         Tab III. New Deal Support and Patriotism                                          ****
****         	a. Panel A                                     							       ****
***************************************************************************************************
qui reghdfe vol C40WPA                                 $controls_mat if sample == 1 & C40SCHOOL_C == 0 & C40SCHOOL_H == 0, vce(cluster countyn) absorb(countyn)
qui gen sample_wpa_controls = e(sample) == 1
	
estimates clear
eststo: xi: reghdfe vol C40WPA                                 i.C40AGE 	 if sample_wpa_controls == 1, vce(cluster countyn) absorb(countyn)
       qui: sum     vol if e(sample) == 1
	estadd local y_mean_round = string(r(mean), "%9.3f")
	estadd local sample       = "$<$ high sch."
	estadd local fe           = "County"
eststo: xi: reghdfe vol C40WPA                                 $controls_mat if sample_wpa_controls == 1, vce(cluster countyn) absorb(countyn)
       qui: sum     vol if e(sample) == 1
	estadd local y_mean_round = string(r(mean), "%9.3f")
	estadd local sample       = "$<$ high sch."
	estadd local fe           = "County"
eststo: xi: reghdfe  vol C40WPA                                $controls_mat if sample == 1 & C40SCHOOL_C == 0 & C40SCHOOL_H == 0 & cem_matched_wpa == 1, vce(cluster countyn) absorb(countyn)
       qui: sum      vol if e(sample) == 1
	estadd local y_mean_round = string(r(mean), "%9.3f")
	estadd local sample       = "$<$ high sch., CEM"
	estadd local fe           = "County"
	
esttab using "results/tables/Tab3_main-a.tex" ,	                                                                              ///
                indicate("Age FEs = *AGE*" "Individual controls = $controls_mat_noage" "Birthplace FEs = *bpl*" )             ///
		mgroups("Volunteer" , pattern(1 0 0)                                                                                  ///
		prefix(\multicolumn{@span}{c}{) suffix(})                                                                             ///
			span erepeat(\cmidrule(lr){@span}) )                                                                          	  ///
		replace br se  label star(* 0.10 ** 0.05 *** 0.01) obslast nomtitles                                                  ///
		scalars("fe Fixed effects" "sample Sample:" "y_mean_round Mean dependent variable")                                   ///
		b(%9.3f) se(%9.3f) r2(%9.3f) collabels(none) nonumber                                                                 ///
		nonotes nogaps nolegend
		
***************************************************************************************************
****         	b. Panel B                                     							       ****
***************************************************************************************************

qui reghdfe vol iHOLC_PH_C40HOMEOWN      C40HOMEOWN             $controls_mat if sample == 1 & c30urban1 == 1                            	   , vce(cluster countyn) absorb(countyn)
qui gen sample_holc_controls = e(sample) == 1

estimates clear
eststo: xi: reghdfe vol iHOLC_PH_C40HOMEOWN      C40HOMEOWN              i.C40AGE 	if sample_holc_controls == 1                            , vce(cluster countyn) absorb(countyn)
	qui: sum        vol if e(sample) == 1
	estadd local y_mean_round = string(r(mean), "%9.3f")
	estadd local sample       = "Urban"
	estadd local fe           = "County"
eststo: xi: reghdfe vol iHOLC_PH_C40HOMEOWN      C40HOMEOWN             $controls_mat if sample_holc_controls == 1                           , vce(cluster countyn) absorb(countyn)
       qui: sum     vol if e(sample) == 1
	estadd local y_mean_round = string(r(mean), "%9.3f")
	estadd local sample       = "Urban"
	estadd local fe           = "County"
eststo: xi: reghdfe  vol iHOLC_PH_C40HOMEOWN      C40HOMEOWN            $controls_mat if sample == 1 & c30urban1 == 1 & cem_matched_homeown == 1, vce(cluster countyn) absorb(countyn)
       qui: sum      vol if e(sample) == 1
	estadd local y_mean_round = string(r(mean), "%9.3f")
	estadd local sample       = "Urban, CEM"
	estadd local fe           = "County"
	
esttab using "results/tables/Tab3_main-b.tex" ,	                                                                              ///
                indicate("Age FEs = *AGE*" "Individual controls = $controls_mat_noage" "Birthplace FEs = *bpl*" )             ///
		mgroups("Volunteer" , pattern(1 0 0)                                                                                  ///
		prefix(\multicolumn{@span}{c}{) suffix(})                                                                             ///
			span erepeat(\cmidrule(lr){@span}) )			                                                                  ///
		replace br se  label star(* 0.10 ** 0.05 *** 0.01) obslast nomtitles  	           			              			  ///
		scalars("fe Fixed effects" "sample Sample:" "y_mean_round Mean dependent variable")                                   ///
		b(%9.3f) se(%9.3f) r2(%9.3f) collabels(none) nonumber                                                                 ///
		nonotes nogaps nolegend

		
***************************************************************************************************
****         	c. Panel C                                    							       ****
***************************************************************************************************

qui reghdfe vol iAAA_PF_farmowner farmowner           $controls_mat if sample_aaa == 1 & C40FARM == 1                       , vce(cluster countyn) absorb(countyn)
qui gen sample_aaa_controls = e(sample) == 1

estimates clear
eststo: xi: reghdfe vol  iAAA_PF_farmowner farmowner 			i.C40AGE 		if sample_aaa_controls == 1                       , vce(cluster countyn) absorb(countyn)
       qui: sum     vol if e(sample) == 1
	estadd local y_mean_round = string(r(mean), "%9.3f")
	estadd local sample       = "Farm"
	estadd local fe           = "County"
eststo: xi: reghdfe vol  iAAA_PF_farmowner farmowner           $controls_mat if sample_aaa_controls == 1                       , vce(cluster countyn) absorb(countyn)
       qui: sum     vol if e(sample) == 1
	estadd local y_mean_round = string(r(mean), "%9.3f")
	estadd local sample       = "Farm"
	estadd local fe           = "County"
eststo: xi: reghdfe  vol iAAA_PF_farmowner farmowner           $controls_mat if sample == 1 & C40FARM == 1 & cem_matched_farmown == 1, vce(cluster countyn) absorb(countyn)
       qui: sum      vol if e(sample) == 1
	estadd local y_mean_round = string(r(mean), "%9.3f")
	estadd local sample       = "Farm, CEM"
	estadd local fe           = "County"

esttab using "results/tables/Tab3_main-c.tex" ,	                                                                              ///
                indicate("Age FEs = *AGE*" "Individual controls = $controls_mat_noage" "Birthplace FEs = *bpl*" )             ///
		mgroups("Volunteer" , pattern(1 0 0)                                                                                  ///
		prefix(\multicolumn{@span}{c}{) suffix(})                                                                             ///
			span erepeat(\cmidrule(lr){@span}) )			                                                                  ///
		replace br se  label star(* 0.10 ** 0.05 *** 0.01) obslast nomtitles  	           			              			  ///
		scalars("fe Fixed effects" "sample Sample:" "y_mean_round Mean dependent variable")                                   ///
		b(%9.3f) se(%9.3f) r2(%9.3f) collabels(none) nonumber                                                                 ///
		nonotes nogaps nolegend
		
***************************************************************************************************		
****         Tab V. Identification Table: Individual-Level                                     ****
****         	a. Col. 1 & 3                                  							       ****
***************************************************************************************************
estimates clear
eststo: xi:      reghdfe  vol iSUM3MO_DROUGHT3340_farmowner iAGRI_T73_farmowner  farmowner $controls_mat 										 if sample_aaa == 1 & C40FARM == 1, cluster(countyn) absorb(countyn)
       qui:      sum  vol if e(sample) == 1
	estadd local y_mean_round = string(r(mean), "%9.3f")
	estadd local fe       	  = "Yes"
	estadd local sample       = "Farm"
	
eststo:  xi: ivreghdfe  vol (iAAA_PF_farmowner = iSUM3MO_DROUGHT3340_farmowner iAGRI_T73_farmowner) farmowner $controls_mat if sample_aaa == 1 & C40FARM == 1, cluster(countyn) absorb(countyn)
	qui:         sum  vol if e(sample) == 1
	estadd local y_mean_round = string(r(mean), "%9.3f")
	estadd local fe       	  = "Yes"
	estadd local sample       = "Farm"
	
esttab using "results/tables/Tab5-id-a.tex" ,	                              	                          ///
indicate("Birthplace FEs = *bpl*" "Individual controls (Census 1940) = $controls_mat_noage") 			  ///
		order(iAAA_PF_farmowner  iSUM3MO_DROUGHT3340_farmowner iAGRI_T73_farmowner farmowner)  		  ///
		mgroups("Volunteer" , pattern(1 0)							       								  ///
		prefix(\multicolumn{@span}{c}{) suffix(})                                                         ///
			span erepeat(\cmidrule(lr){@span}) )			                               				  ///
		mlabel("RF" "2SLS" )                                                              			 	  ///
	scalars("fe County FEs" "y_mean_round Mean dependent variable" "sample Sample:")  					  ///
	replace br se  label star(* 0.10 ** 0.05 *** 0.01) obslast nomtitles nonum compress 	              ///
	b(%9.3f) se(%9.3f) r2(%9.3f)  nonotes                                                                        ///
	nogaps title("Identification Table: Individual-Level.")

	
***************************************************************************************************
****     								B. Appendix                                            ****
****     I. Tables                                                                             ****	
****         Tab 7. Impact of WPA Employment and Unemployment                                  ****
***************************************************************************************************
qui reghdfe vol C40WPA  C40UNEMP                               $controls_mat if sample == 1 & C40SCHOOL_C == 0 & C40SCHOOL_H == 0, vce(cluster countyn) absorb(countyn)
qui gen sample_wpa_unemp = e(sample) == 1
	
estimates clear
eststo nocon: xi: reghdfe vol C40WPA  C40UNEMP                        i.C40AGE 	 if sample_wpa_unemp == 1, vce(cluster countyn) absorb(countyn)
       qui: sum     vol if e(sample) == 1
	estadd local y_mean_round = string(r(mean), "%9.3f"):nocon
		qui: test C40WPA = C40UNEMP
	estadd local Ttest		  = string(r(p), "%9.3f" ) : nocon
	estadd local sample       = "$<$ high sch.": nocon
	estadd local fe           = "County":nocon
	
eststo con: xi: reghdfe vol C40WPA    C40UNEMP                      $controls_mat if sample_wpa_unemp == 1, vce(cluster countyn) absorb(countyn)
       qui: sum     vol if e(sample) == 1
	estadd local y_mean_round = string(r(mean), "%9.3f"): con
		qui: test C40WPA = C40UNEMP
	estadd local Ttest		  = string(r(p), "%9.3f" ) : con
	estadd local sample       = "$<$ high sch.": con
	estadd local fe           = "County": con
	
esttab using "results/tables/AppTab7_unempl.tex" ,	                                                                          ///
        indicate("Age FEs = *AGE*" "Individual controls = $controls_mat_noage" "Birthplace FEs = *bpl*" )             		  ///
		mgroups("Volunteer" , pattern(1 0)                                                                                    ///
		prefix(\multicolumn{@span}{c}{) suffix(})                                                                             ///
			span erepeat(\cmidrule(lr){@span}) )                                                                          	  ///
		replace br se  label star(* 0.10 ** 0.05 *** 0.01) obslast nomtitles                                                  ///
		scalars("fe Fixed effects" "sample Sample:" "y_mean_round Mean dependent variable" "Ttest Works for WPA = Unemployed (p-value)") ///
		b(%9.3f) se(%9.3f) r2(%9.3f) collabels(none) nonumber                                                                 ///
		nonotes nogaps nolegend

***************************************************************************************************	
****     II. Figures                                                                           ****	
****         Fig 8. Plausibly Exogenous (Conley et al., 2012)                                  ****
***************************************************************************************************

estimates clear 
xi: reghdfe vol iSUM3MO_DROUGHT3340_farmowner  iAGRI_T73_farmowner farmowner $controls_mat if sample_aaa == 1 & C40FARM == 1, cluster(countyn) absorb(countyn)
gen sample_ols_aaa = e(sample) ==1

local level 0.9
foreach instrument in iSUM3MO_DROUGHT3340_farmowner iAGRI_T73_farmowner{
	if "`instrument'" == "iAGRI_T73_farmowner"           local instrument_title committee
	if "`instrument'" == "iAGRI_T73_farmowner"           local explanation      "Tenure in Agriculture committee (i.h.s.): 1933-35 {c 215} farm owner"
	if "`instrument'" == "iAGRI_T73_farmowner"           local ivs              "iSUM3MO_DROUGHT3340_farmowner iAGRI_T73_farmowner"
	if "`instrument'" == "iSUM3MO_DROUGHT3340_farmowner" local instrument_title drought
	if "`instrument'" == "iSUM3MO_DROUGHT3340_farmowner" local explanation      "Summer droughts (i.h.s.): 1933-40 {c 215} farm owner"
	if "`instrument'" == "iSUM3MO_DROUGHT3340_farmowner" local ivs              "iAGRI_T73_farmowner iSUM3MO_DROUGHT3340_farmowner"
	

	if "`instrument'" == "iSUM3MO_DROUGHT3340_farmowner" local           title "A. Farm owner"
	if "`instrument'" == "iAGRI_T73_farmowner"           local           title "D. Farm owner"
	if "`instrument'" == "iSUM3MO_DROUGHT3340_farmowner" local           lettr "A"
	if "`instrument'" == "iAGRI_T73_farmowner"           local           lettr "D"

		if "`instrument'" == "iSUM3MO_DROUGHT3340_farmowner" 															local           gn 8
		if "`instrument'" == "iAGRI_T73_farmowner"  																	local           gn 600
		if "`instrument'" == "iSUM3MO_DROUGHT3340_farmowner" 															local           gx 1.4
		if "`instrument'" == "iAGRI_T73_farmowner"  																	local           gx 100
		
		xi: reghdfe vol `ivs' farmowner $controls_mat if sample_ols_aaa==1, cluster(countyn) absorb(countyn)
		local b  = _b[`instrument']
		local gn = -1 * `gn' * `b'
		local gx =      `gx' * `b'
		
		local rn = round(`gn',0.1)
		local rx = round(`gx',0.1)
		
		ivreghdfe  vol (iAAA_PF_farmowner = `ivs') farmowner $controls_mat if sample_ols_aaa==1, absorb(countyn) cluster(countyn)
		local CI  = -invnormal((1 - `level')/2)
		local liv = _b[iAAA_PF_farmowner] - `CI'*_se[iAAA_PF_farmowner]
		local uiv = _b[iAAA_PF_farmowner] + `CI'*_se[iAAA_PF_farmowner]
		
		plausexog uci vol $controls_mat (iAAA_PF_farmowner = `ivs') i.countyn if sample_ols_aaa==1, vce(cluster countyn) gmin(0 `gn') gmax(0 `gx') level(`level')
		
		local upper1 = max(`uiv', e(ub_iAAA_PF_farmowner))
		local upper2 = min(`uiv', e(ub_iAAA_PF_farmowner))
		
		preserve
			clear
			set obs 3
			gen x   = cond(_n == 1,       -0.5 , 								///
			          cond(_n == 2,         0   , 								///
					  cond(_n == 3,        3 ,.)))
			gen lci = cond(_n == 1,      `liv', 								///
			          cond(_n == 2,      `liv', 								///
					  cond(_n == 3, 	 e(lb_iAGRI_PF_farmowner),.)))
			gen uci = cond(_n == 1, 	`upper1', 								///
			          cond(_n == 2,     `upper2', 								///
					  cond(_n == 3,     `upper2',.)))
					  
			twoway (line lci x, lpattern(dash) lcolor(black)) ///
			       (line uci x, lpattern(dash) lcolor(black)), ytitle("Estimated {&beta} for AAA grants {c 215} farm owner") xtitle({&delta}) xline(`b', lcolor(blue)) yline(0, lcolor(red)) ///
				                                               ylabel( , angle(0) format(%9.1f)) xlabel(-0.5(0.5)3, format(%9.2f)) ///
															   legend(order(1 "Union of confidence intervals")) title(`title', size(huge)) note(Instrument is: `explanation'.)  graphregion(color(white) lwidth(large))
		
		graph export "results/figures/AppFig8`lettr'-conley2012-aaa_farmown-`instrument_title'.pdf", replace as(pdf)
		restore
} /* foreach instrument */	

rmdir "tmp/"

log close
exit
